package com.winit.config.repository;

import com.datastax.driver.core.querybuilder.QueryBuilder;
import com.datastax.driver.core.querybuilder.Select;
import com.winit.config.model.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.cassandra.core.CassandraOperations;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.util.List;

@Repository
public class ConfigRepositoryImpl implements SuperConfigRepository {

    @Autowired
    private CassandraOperations cassandraOperations;

    @Override
    public List<Config> findByConditions(Config config) {
        Select select = QueryBuilder.select().from("uconfig_config");
        if(!StringUtils.isEmpty(config.getConfig_id())){
            select.where(QueryBuilder.eq("config_id", config.getConfig_id()));
        }
        if(!StringUtils.isEmpty(config.getConfig_app_tag())){
            select.where(QueryBuilder.eq("config_app_tag", config.getConfig_app_tag()));
        }
        if(!StringUtils.isEmpty(config.getConfig_zone_tag())){
            select.where(QueryBuilder.eq("config_zone_tag", config.getConfig_zone_tag()));
        }
        if(!StringUtils.isEmpty(config.getConfig_env_tag())){
            select.where(QueryBuilder.eq("config_env_tag", config.getConfig_env_tag()));
        }
        if(!StringUtils.isEmpty(config.getConfig_type_tag())){
            select.where(QueryBuilder.eq("config_type_tag", config.getConfig_type_tag()));
        }
        if (!StringUtils.isEmpty(config.getConfig_resource_tag())){
            select.where(QueryBuilder.eq("config_resource_tag", config.getConfig_resource_tag()));
        }
        if (!StringUtils.isEmpty(config.getConfig_version())){
            select.where(QueryBuilder.eq("config_version", config.getConfig_version()));
        }
        select.allowFiltering();
        List<Config> list = cassandraOperations.select(select, Config.class);
        return list;
    }
}
